// pages/tech/field-service/feedback/feedback.js
//调用方式 / pages/tech/feedback/feedback?appid=123&recordid=852&satisfaction={xxx}&requestname=ddddxx
var httpUtil = require('../../../utils/httpUtil.js')
var uiUtil = require('../../../utils/uiUtil.js')
var emptyUtil = require('../../templates/empty/empty.js')
const app = getApp()
var that
Page({

  /**
   * 页面的初始数据
   */
  data: {
    appId: null,
    recordId: null,
    satisfaction: null,
    requestName: null,

    selectedKeyList: [],
    appraise: null,
    suggestion: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    that = this;
    var appId = options.appid
    var recordId = options.recordid
    var satisfaction = JSON.parse(decodeURIComponent(options.satisfaction))
    var requestName = options.requestname

    if (satisfaction && satisfaction.optiion) {
      var length = satisfaction.optiion.length;
      for (var i = 0; i < length; i++) {
        var item = satisfaction.optiion[i]
        item.checked = '0'
      }
    }

    this.setData({
      appId: appId,
      recordId: recordId,
      satisfaction: satisfaction,
      requestName: requestName
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return util.handleShareApp()
  },

  /**
   *满意程度点击
   */
  onSatisfactionClick(e) {
    var index = e.currentTarget.dataset.index
    var satisfaction = this.data.satisfaction
    var optiionList = satisfaction.optiion
    if (satisfaction.type == 'radio') { //单选

      for (var i = 0; i < optiionList.length; i++) {
        var optiion = optiionList[i]
        var lastIsSelected = optiion.checked == '1' //上次是否已选中
        if (index == i) { //当前点击的item
          optiion.checked = lastIsSelected ? '0' : '1'

          //设置选中列表
          if (lastIsSelected) {
            this.setData({
              selectedKeyList: []
            })
          } else {
            this.setData({
              selectedKeyList: [optiion.key]
            })
          }

        } else if (optiion.checked == '1') { //之前为已选中状态
          optiion.checked = '0'
        }
      }
    } else { //多选
      var optiion = optiionList[index]
      var lastIsSelected = optiion.checked == '1' //上次是否已选中
      optiion.checked = lastIsSelected ? '0' : '1'

      var selectedKeyList = this.data.selectedKeyList

      if (lastIsSelected) { //之前为已选中状态
        var checkIndex = selectedKeyList.indexOf(optiion.key);
        if (checkIndex > -1) {
          selectedKeyList.splice(checkIndex, 1);
        }
      } else {
        selectedKeyList.push(optiion.key)
      }

      this.setData({
        selectedKeyList: selectedKeyList
      })

    }

    this.setData({
      satisfaction: satisfaction
    })

  },

  /**
   * 详细描述输入
   */
  onAppraiseInput(e) {
    this.setData({
      appraise: e.detail.value,
    })
  },

  /**
   * 建议输入
   */
  onSuggestionInput(e) {
    this.setData({
      suggestion: e.detail.value,
    })
  },


  /**
   * 提交数据
   */
  onSubmitBtnClick() {
    uiUtil.showLoading("请稍候...")
    var selectedKeyList = this.data.selectedKeyList
    if (selectedKeyList.length == 0) {
      uiUtil.showToast("请选择满意程度")
      return
    }

    var appraise = this.data.appraise
    if (!appraise) {
      uiUtil.showToast("请输入详细描述")
      return
    }

    var suggestion = this.data.suggestion
    if (!suggestion) {
      uiUtil.showToast("请输入建议")
      return
    }

    var selectedKeyString = '';
    var flag = false;
    for (var i = 0; i < selectedKeyList.length; i++) {
      var key = selectedKeyList[i]
      if (flag) {
        selectedKeyString = selectedKeyString + ',';
      } else {
        flag = true;
      }
      selectedKeyString = selectedKeyString + key;
    }

    var requestName = that.data.requestName;

    httpUtil.PostRequestHeaderApp(requestName, {
      appId: that.data.appId,
      recordId: that.data.recordId,
      satisfaction: selectedKeyString,
      appraise: appraise,
      suggestion: suggestion
    }, {
      success(res) {
        uiUtil.showToast("提交成功")

        //回调
        try {
          var pages = getCurrentPages()
          var listPage = pages[pages.length - 3]

          listPage.onResultFeedBackOk(that.data.recordId, that.data.appId)
        } catch (e) {}

        //延迟返回
        setTimeout(function() {
          wx.navigateBack({
            delta: 2
          })
        }, 600)

      },
      fail(code, res) {
        var text = emptyUtil.getErrText(code, res, "提交反馈失败")
        uiUtil.showToast(text)
      },
      complete() {
        uiUtil.hideLoading()
      }
    })
  },

})